﻿/**

 @Name：ajax 数据请求模块
 @Author：Zxq
 @License：MIT
    
 */
layui.define('jquery', function (exports) {
    "use strict";

    var $ = layui.$;

    //构造函数
    var Ajax = function(){
        var create = function (options) {
            var ind = -1;
            $.ajax({
                url: options.url,
                type: options.type || "get",
                data: options.data || {},
                dataType: options.dataType || "json",
                async: options.async == null ? true : options.async,
                beforeSend: function(XMLHttpRequest) {
                    if (typeof options.beforeSend === 'function') {
                        if (!options.beforeSend(XMLHttpRequest))
                            return false;
                    }
                    if (options.header == null ? true : options.header) {
                        var session = layui.data('swui', 'session');
                        if (session && session.Token && session.ActionKey) {
                            XMLHttpRequest.setRequestHeader('Authorization', "Bearer "+session.Token);
                            XMLHttpRequest.setRequestHeader('ActionKey', session.ActionKey);
                        }
                    }
                    if (options.loadmsg != null) {
                        //layxwin.load('loading',options.loadmsg || '数据加载中',{width:200,height:50,shadable:0.3,zIndex:999999999});
                        if (options.loadmsg === '') {
                            ind = layer.load(2, { zIndex: 999999999 });
                        }
                        else ind = layer.msg(options.loadmsg, { icon: 16, shade: 0.3, time: 0, zIndex: 999999999 });
                    }
                },
                success: function (data, textStatus) {
                    if ((options.header == null ? true : options.header) && (data.Code === 401 || data.Code === 403)) {
                        layer.confirm('未登录或已超时，是否重新登录？',{icon: 3, title:'提示'}, function(index){
                            layer.close(index);
                            window.location.href = swui && swui.setter ? swui.setter.Page.login : 'login.html';
                        });
                        return;
                    }
                    if (data.ActionKey || data.Token) {
                        var session = layui.data('swui', 'session') || {};
                        if (data.ActionKey) session.ActionKey = data.ActionKey;
                        if (data.Token) session.Token = data.Token;
                        layui.data('swui', {key:'session',value:session});
                    }
                    if (typeof options.success === 'function')
                        options.success(data, textStatus);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    if (typeof options.error === 'function')
                        options.error(XMLHttpRequest, textStatus, errorThrown);
                    else {
                        layer.open({
                            title: '错误'
                            ,content: XMLHttpRequest.responseText
                            ,icon: 2
                        });
                    }
                },
                complete: function (XMLHttpRequest, textStatus) {
                    //layxwin.destroy('loading');
                    if (options.loadmsg != null)
                        layer.close(ind);
                }
            })
        };
        this.init = function (url, method, data, params, success, error) {
            if (!url) return;
            if (url.substr(url.length-5)==='.json')
                method = 'get';
            create({
                url: url,
                type: method,
                data: data,
                dataType: params.dataType,
                async: params.async,
                header: params.header,
                loadmsg: params.loadmsg,
                beforeSend: params.beforeSend,
                success: success,
                error: error
            })
        };
    };

    var ajax = new Ajax();

    var methods = [
        {
          name: 'html',
          method: 'get',
          async: true,
          dataType: 'html'
        },
        {
          name: 'get',
          method: 'get',
          async: true,
          dataType: 'json'
        },
        {
          name: 'post',
          method: 'post',
          async: true,
          dataType: 'json'
        },
        {
          name: 'syncGet',
          method: 'get',
          async: false,
          dataType: 'json'
        },
        {
          name: 'syncPost',
          method: 'post',
          async: false,
          dataType: 'json'
        }
    ];

    for (var i = 0, l = methods.length; i < l; i++) {
        ajax[methods[i].name] = (function (i) {
            return function () {
                var _url = arguments[0],
                _data = arguments[1],
                _params = arguments[2] || {};
                _params.async = methods[i].async;
                _params.dataType = methods[i].dataType;
        
                return ajax.init(_url, methods[i].method, _data, _params, arguments[3], arguments[4]);
            }
        })(i);
    }
    
    window.ajax = ajax;

    exports('ajax', {});

    delete layui.ajax;
});